package mes.kanban.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import mes.kanban.config.KVRedisConfig;
import mes.kanban.entity.MesMaterial;
import mes.kanban.entity.SelectVo;
import mes.kanban.exception.MyException;
import mes.kanban.service.MaterialServiceI;
import mes.kanban.util.GetSessionUtil;
import mes.kanban.util.PageBean;
import mes.kanban.util.UploadFileToServerUtil;
import mes.kanban.vo.AjaxReturnVO;
import mes.kanban.vo.MesMaterialVO;
import mes.kanban.vo.MesUserVO;

@RequestMapping(value="/material")
@Controller
public class MaterialController {
	
	@Autowired
	private MaterialServiceI materialServiceI;
	
	@Autowired
	private UploadFileToServerUtil uploadFileToServerUtil;
	
	
	@RequestMapping(value="/toAdd")
	public String gotoUploadMaterial() {
		return "/material/material_uploda";
	}
	
	@RequestMapping(value="/toConfig")
	public String gotoMaterialConfig() {
		
		return "/material/material_config";
	}
	
	@RequestMapping(value="/toManage")
	public String gotoMaterialManage() {
		
		return "/material/material_manage";
	}
	
	//返回值直接就是uploadSuccess方法的response
	@PostMapping(value="/upload")
	@ResponseBody
	public String uploadFileToServer(@RequestParam("file")MultipartFile file,HttpServletRequest request,@RequestParam("id")String id,@RequestParam("companyId")String companyId  )  {
		
		 String contentType = file.getContentType();
//	     double size = new BigDecimal(file.getSize()/ 1024).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
	     String origName = file.getOriginalFilename();
	     
	     try {
	    	 String sizeType = "";
	    	 String matTypeDesc = "";
	    	 if("1".equals(id)){
	    		 matTypeDesc = "商店精品";
	    	 }else if("2".equals(id)){
	    		 matTypeDesc = "首页展示";
	    		 sizeType = companyId;
	    	 }else if("3".equals(id)){
	    		 matTypeDesc = "热销产品";
	    	 }else if("4".equals(id)){
	    		 matTypeDesc = "全部产品";
	    	 }
	    	 
	    	 Map<String, String> fileMap =  uploadFileToServerUtil.uploadToWindows(file);
	    	 
	    	 HttpSession session = WebUtils.toHttp(request).getSession();
	    	 
	    	 String userinfoID = String.valueOf(session.getAttribute(KVRedisConfig.SESSION_ATTRIBUTE_USERID.getHaskey()));
	    	 
	    	 String userinfoName = String.valueOf(session.getAttribute(KVRedisConfig.SESSION_ATTRIBUTE_USERNAME.getHaskey()));
	    	 
	    	 MesMaterial m = new MesMaterial();
	    	 
	    	 m.setCreateTime(new Date());
	    	 m.setCreator(userinfoID);
	    	 m.setCreatorName(userinfoName);
	    	 m.setMaterialName(fileMap.get("fileName"));
	    	 m.setUrl(fileMap.get("host"));
	    	 m.setName(origName);
	    	 m.setMemo(contentType);
	    	 m.setCreatorType(id);
	    	 m.setCreatorTypeDesc(matTypeDesc);
	    	 m.setHdc_businesslocation(sizeType);
	    	 
	    	 materialServiceI.add(m);
	    	 
	    	 return "success";
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	     
	}


	
	/**
	 * 查询素材列表
	 * @param request
	 * @return
	 */
	@GetMapping(value="/list")
	@ResponseBody
    public AjaxReturnVO list(HttpServletRequest request) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		try {
			
			HttpSession session = WebUtils.toHttp(request).getSession();
			Object info = session.getAttribute("userSession");
			MesUserVO userInfo = new MesUserVO();
			BeanUtils.copyProperties(info, userInfo);
			List<MesMaterial> datas = materialServiceI.getDatas(userInfo.getId());
			//List<MesMaterial> datas = materialServiceI.getDatas();
			j.setData(datas);
			j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
		
        return j;
        
    }
	
	/**
	 * 分页查询素材列表
	 * @param request
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value="/pageList")
	@ResponseBody
    public AjaxReturnVO list(HttpServletRequest request,@RequestParam("pageNum")Integer pageNum,
    		@RequestParam("pageSize")Integer pageSize) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			HttpSession session = WebUtils.toHttp(request).getSession();
			Object info = session.getAttribute("userSession");
			MesUserVO userInfo = new MesUserVO();
			BeanUtils.copyProperties(info, userInfo);
			PageBean datas = materialServiceI.getDatasForPageBean(pageNum,pageSize,userInfo.getId());
			//PageBean datas = materialServiceI.getDatasForPageBean(pageNum,pageSize);
			j.setData(datas);
			j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
		
        return j;
        
    }
	
	
	/**
	 * 分页查询素材列表
	 * @param request
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value="/toSearch")
	@ResponseBody
    public AjaxReturnVO toSearch(HttpServletRequest request,@RequestParam("pageNum")Integer pageNum,
    		@RequestParam("pageSize")Integer pageSize,@RequestParam("name")String name ,@RequestParam("type")String type) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			HttpSession session = WebUtils.toHttp(request).getSession();
			Object info = session.getAttribute("userSession");
			MesUserVO userInfo = new MesUserVO();
			BeanUtils.copyProperties(info, userInfo);
			PageBean datas = materialServiceI.toSearch(pageNum,pageSize,userInfo.getId(),name,type);
			//PageBean datas = materialServiceI.getDatasForPageBean(pageNum,pageSize);
			j.setData(datas);
			j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
		
        return j;
        
    }
	
	@GetMapping(value="/getMateriaById")
	@ResponseBody
	public AjaxReturnVO getMaterialById(@RequestParam(value="id")String id) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			MesMaterialVO materialVo = materialServiceI.findMaterialById(id);
			j.setOk(true);
			j.setData(materialVo);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
		return j;
	}
	
	@PostMapping(value="/update")
	@ResponseBody
	public AjaxReturnVO update(@RequestBody MesMaterial mesMaterial) {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		try {
			vo.setOk(true);
			this.materialServiceI.update(mesMaterial);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
		}
		
		return vo;
		
	}
	
	
	/**
	 * 分页查询素材列表
	 * @param request
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	
	@GetMapping(value="/getcompy")
	@ResponseBody
    public AjaxReturnVO getcompy(HttpServletRequest request,@RequestParam("pageNum")Integer pageNum,
    		@RequestParam("pageSize")Integer pageSize) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			HttpSession session = WebUtils.toHttp(request).getSession();
			Object info = session.getAttribute("userSession");
			MesUserVO userInfo = new MesUserVO();
			BeanUtils.copyProperties(info, userInfo);
			List<SelectVo> datas = materialServiceI.getcompy("","");
			j.setData(datas);
			j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
		
        return j;
        
    }
	
	@GetMapping(value="/deleteMateria")
	@ResponseBody
	public AjaxReturnVO deleteMateria(HttpServletRequest request,@RequestParam("id")String id) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		try {
			String UserId = GetSessionUtil.getUserInfoToOther(request, KVRedisConfig.SESSION_ATTRIBUTE_USERID.getHaskey());
			this.materialServiceI.deleteMaterialById(id,UserId);
			j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
			if (e instanceof MyException) {
				j.setMsg("报错了:"+e.getMessage());
			}else {
				j.setMsg("其他异常");
			}
			
		}
		
		return j;
	}
	
	
}
